﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Web;
using Sausage.Data;

namespace Sausage.Domain
{
    public class UserPrincipal: GenericPrincipal
    {
        #region Properties
        private Guid _userID;
        public Guid UserID
        {
            get { return _userID; }
        }

        private Role _role;
        public Role Role
        {
            get
            {
                if (_role == null)
                    _role = RoleHelper.GetDefaultRoleByUser(User);
                return _role;
            }
        }

        private User _user;
        public User User
        {
            get
            {
                if (_user == null)
                    _user = UserHelper.GetUserByID(_userID);
                return _user;
            }
        }

        private RoleType _roleType = RoleType.Unknow;
        public RoleType RoleType
        {
            get
            {
                if (_roleType == RoleType.Unknow)
                    _roleType = RoleHelper.GetRoleType(Role);
                return _roleType;
            }
        }

        private List<MenuInfo> _meanList;
        public List<MenuInfo> MeanList
        {
            get
            {
                if (_meanList == null)
                {
                    _meanList = MenuHelper.GetMenuListByRole(this.Role);
                }
                return _meanList;
            }
        }

        public bool IsAdmin
        {
            get { return RoleType == RoleType.SectionAdmin || RoleType == RoleType.DistrictAdmin; }
        }

        public bool IsUser
        {
            get { return RoleType == RoleType.MaintainUser || RoleType == RoleType.ProjectUser; }
        }

        public Organization Organization
        {
            get { return User.Organization; } // ?
        }
        #endregion

        public UserPrincipal(IIdentity identity, string[] roles) : base(identity, roles)
        {
            _userID = Guid.Parse(this.Identity.Name);
        }
    }
}
